//
//  FriendViewController.m
//  BuffetNews
//
//  Created by 大大大鸿哥 on 14/11/10.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FriendViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "FriendCell.h"
#import "UIImageView+WebCache.h"
#import <SinaWeiboConnection/SSSinaWeiboUser.h>
#import "WebColor.h"
#import "NightModel.h"
#import "FriendWebViewController.h"

@interface FriendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *friendTV;

@end
@implementation FriendViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.friendArr = [NSArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.friendTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    self.friendTV.dataSource = self;
    self.friendTV.delegate = self;
    self.friendTV.rowHeight = 70;

    self.friendTV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.friendTV];
    
    if ([NightModel shareInstance].open == 1) {
        self.friendTV.backgroundColor = [WebColor dimGray];
    } else {
        self.friendTV.backgroundColor = [WebColor whiteSmoke];
    }
    //  返回首页按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, self.view.frame.size.height - 30, 40, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"btn_home.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
    
    
    [ShareSDK getFriendsWithType:ShareTypeSinaWeibo page:nil authOptions:nil result:^(SSResponseState state, NSArray *users, long long curr, long long prev, long long next, BOOL hasNext, NSDictionary *extInfo, id<ICMErrorInfo> error) {
        NSLog(@"users : %@", users);
        NSLog(@"curr : %lld", curr);
        NSLog(@"prev : %lld", prev);
        NSLog(@"next : %lld", next);
        NSLog(@"hasNext : %d", hasNext);
        NSLog(@"extInfo : %@", extInfo);
        self.friendArr = [NSArray arrayWithArray:users];

        [self.friendTV reloadData];
    }];
    [_friendTV release];
}
- (void)buttonClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.friendArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friend"];
    if (cell == nil) {
        cell = [[[FriendCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"friend"]autorelease];
    }

    SSSinaWeiboUser *sinaUser = [self.friendArr objectAtIndex:indexPath.row];
    
    
    cell.myLabel.text = sinaUser.nickname;
    [cell.myImgView sd_setImageWithURL:[NSURL URLWithString:sinaUser.profileImage]];
    cell.myImgView.layer.cornerRadius = 25;
    cell.myImgView.clipsToBounds = YES;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SSSinaWeiboUser *sinauser = [self.friendArr objectAtIndex:indexPath.row];
    FriendWebViewController *friendWebVC = [[FriendWebViewController alloc] init];
    friendWebVC.apiString = sinauser.url;
    [friendWebVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:friendWebVC animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
